package com.test.controller;

import com.github.pagehelper.PageInfo;
import com.test.domain.Dept;
import com.test.service.DeptService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/dept")
public class DeptController {

	@Resource
	DeptService deptService;
	
	@Resource
	HttpServletRequest request;
	
	/**
	 * 跳转-部门列表
	 * @return
	 */
	@RequestMapping("/list")
	public String deptListPage(Dept dept){
		PageInfo<Dept> pageInfo=deptService.selectForPageInfo(dept);
		request.setAttribute("pageInfo", pageInfo);
		return "deptlist";
	}
	
	
	@RequestMapping("/delete")
	public String delete(Integer id){
		int deleted = deptService.delete(id);
		if(deleted==0){
		
		}
		return "redirect:/dept/list";
	}
	
	@RequestMapping("/info")
	public String info(Integer id){
		//如果有ID，说明是修改的跳转页面，需要把选择的修改对象内容传递到修改页面
		if(id!=null){
			Dept dept = deptService.selectOne(id);
			System.out.println(dept);
			request.setAttribute("dept", dept);
		}
		return "/deptinfo";
	}
	
	@RequestMapping("/addOrUpdate")
	public String addOrUpdate(Dept dept){
		int result=0;
		if(dept!=null && dept.getId()!=null){
			result = deptService.update(dept);
		}else{
			result = deptService.add(dept);
		}
		if(result==0){
		
		}
		return "redirect:/dept/list";
	}
	
}